package com.blogsoho.taurus.dao.builder;

import com.blogsoho.taurus.vo.CustomerVO;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * Created by 16978 on 2017/6/11.
 */
public class CustomerSqlBuilder {

    private static final String SELECT_FIELD = "id,name,phone,company,url,alert_date,enable_alert,remark,create_time";
    private static final String TAURUS_CUSTOMER = "taurus_customer";

    public String addCustomer(CustomerVO customer) {
        return new SQL() {{
            INSERT_INTO(TAURUS_CUSTOMER);
            VALUES("name", "#{name}");
            VALUES("phone", "#{phone}");
            VALUES("company", "#{company}");
            VALUES("url", "#{url}");
            VALUES("alert_date", "#{alertDate}");
            VALUES("enable_alert", "#{enableAlert}");
            VALUES("remark", "#{remark}");
            if (customer.getCreateTime() != null) {
                VALUES("create_time", "#{createTime}");
            }
        }}.toString();
    }

    public String updateCustomer(CustomerVO customer) {
        return new SQL() {{
            UPDATE(TAURUS_CUSTOMER);

            SET("name = #{name}");
            SET("phone = #{phone}");
            SET("company = #{company}");
            SET("url = #{url}");
            SET("alert_date = #{alertDate}");
            SET("enable_alert = #{enableAlert}");
            SET("remark = #{remark}");
            WHERE("id=" + customer.getId());
        }}.toString();
    }

    public String getAllCustomer(Map<String, Object> para) {
        String sql = new SQL() {{
            SELECT(SELECT_FIELD);
            FROM(TAURUS_CUSTOMER);

        }}.toString();
        sql = sql + " limit " + para.get("start") + "," + para.get("length");
        return sql;
    }

    public String getCountCustomer() {
        return new SQL() {{
            SELECT("count(*)");
            FROM(TAURUS_CUSTOMER);
        }}.toString();
    }

    public String getCustomerById(Map<String, Object> para) {
        return new SQL() {{
            SELECT(SELECT_FIELD);
            FROM(TAURUS_CUSTOMER);
            WHERE("id=" + para.get("id"));
        }}.toString();
    }

    //select * from taurus_customer where alert_date < DATE_ADD(CURDATE(),INTERVAL 3 DAY)

    public String getCustomerByAlertTime(Map<String, Integer> para) {
        return new SQL() {{
            SELECT(SELECT_FIELD);
            FROM(TAURUS_CUSTOMER);
            WHERE("alert_date < DATE_ADD(CURDATE(),INTERVAL " + para.get("day").toString() + " DAY)");
            ORDER_BY("alert_date");
        }}.toString();
    }
}
